#ifndef BUFFER_POOL_H
#define BUFFER_POOL_H

#include "storage/page.h"
#include "storage/disk_manager.h"
#include <unordered_map>
#include <list>
#include <memory>
#include <functional>
#include <string>
#include <iostream>

class BufferPool {
public:
    BufferPool(size_t capacity, std::unique_ptr<DiskManager> diskManager);
    ~BufferPool();

    Page* getPage(uint32_t pageId);
    Page* newPage();
    void flushPage(uint32_t pageId);
    void flushAll();

    size_t getHitCount() const;
    size_t getMissCount() const;
    void printStats() const;
    
    // Set custom log output function
    void setLogFunction(std::function<void(const std::string&)> logFunction);

private:
    void evict();

    size_t capacity;
    size_t hitCount;
    size_t missCount;
    
    // Default log output function is std::cout
    std::function<void(const std::string&)> logFunc = [](const std::string& msg) { std::cout << msg << std::endl; };

    std::unique_ptr<DiskManager> diskManager;
    std::unordered_map<uint32_t, std::unique_ptr<Page>> pages;
    std::list<uint32_t> lruList;
};

#endif // BUFFER_POOL_H